package pkg.orange.online;

/*
 * Obsługa aktywności pozwalającej użytkownikowi na skonfigurowanie widżetu.
 */

import java.io.File;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
//import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;


public class OrangeConfigureWidget extends Activity {
	
	public static final String PREFS_NAME = "OrangeWidgets";

	private SharedPreferences config;
	private SharedPreferences globalCfg;
	private SharedPreferences previousCfg;
	private int appWidgetId;
	private EditText login;
	private EditText password;
	private EditText[] formNames;
	private EditText[] displayNames;
	private Button apply;
	private Context context;
	
	@Override
    public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		//Log.d("OrangeConfigureWidget", "Zaczynam...");
		
		setResult(RESULT_CANCELED);
		 
		// Ustawiamy nasz layout
        setContentView(R.layout.configure_layout);
        
        //Przygotowujemy referencje do przycisków
        login = (EditText) findViewById(R.id.TextLogin);
        password = (EditText) findViewById(R.id.TextPassword);
        formNames = new EditText[6];
        displayNames = new EditText[6];
        formNames[0]=(EditText) findViewById(R.id.TextFormName1);	
        formNames[1]=(EditText) findViewById(R.id.TextFormName2);	
        formNames[2]=(EditText) findViewById(R.id.TextFormName3);	
        formNames[3]=(EditText) findViewById(R.id.TextFormName4);	
        formNames[4]=(EditText) findViewById(R.id.TextFormName5);	
        formNames[5]=(EditText) findViewById(R.id.TextFormName6);	
        displayNames[0]=(EditText) findViewById(R.id.TextDisplayName1);
        displayNames[1]=(EditText) findViewById(R.id.TextDisplayName2);
        displayNames[2]=(EditText) findViewById(R.id.TextDisplayName3);
        displayNames[3]=(EditText) findViewById(R.id.TextDisplayName4);
        displayNames[4]=(EditText) findViewById(R.id.TextDisplayName5);
        displayNames[5]=(EditText) findViewById(R.id.TextDisplayName6);
        apply = (Button) findViewById(R.id.ButtonApply);
        context = this;
        
        //Jeżeli nie ma id widżetu, to konczymy
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
        if (extras != null) {
        	appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
                    AppWidgetManager.INVALID_APPWIDGET_ID);
        }
        if (appWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
            finish();
        }
        
        //Przygotowujemy globalny obiekt z id ostatniego widzetu
        globalCfg = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);   
        int lastId = globalCfg.getInt("lastId", -1);
        if(lastId!=-1){ //przepisujemy ostatnie ustawienia
        	previousCfg = getSharedPreferences(PREFS_NAME+Integer.toString(lastId), MODE_PRIVATE);
        	login.setText(previousCfg.getString("login", ""));
        	password.setText(previousCfg.getString("password", ""));
        	for(int i=0; i<6; i++){
        		formNames[i].setText(previousCfg.getString("formName"+Integer.toString(i), ""));
        		displayNames[i].setText(previousCfg.getString("displayName"+Integer.toString(i), ""));
        	}
        }
        
        //obsługa przycisku "Gotowe"
        apply.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	//przygotowanie opcji - każda instancja będzie miała własne opcje,
            	//można dodać kilka widżetów jednocześnie
            	//Log.d("OrangeConfigureWidget","Tworzę ustawienia "+ PREFS_NAME+Integer.toString(appWidgetId));
            	config = getSharedPreferences(PREFS_NAME+Integer.toString(appWidgetId),0);
            	SharedPreferences.Editor configEditor = config.edit();
            	configEditor.putString("login", login.getText().toString());
            	configEditor.putString("password", password.getText().toString());
            	for(int i=0; i<6; i++){
            		configEditor.putString("formName"+Integer.toString(i), formNames[i].getText().toString());
            		configEditor.putString("displayName"+Integer.toString(i), displayNames[i].getText().toString());
            	}
            	configEditor.putBoolean("updateReady", true);
            	configEditor.commit();
            	
            	//usuwamy ostatnie ustawienia
        	    int lastId = globalCfg.getInt("lastId", -1);
                if(lastId!=-1){
                	String delPrefsName = OrangeConfigureWidget.PREFS_NAME+Integer.toString(lastId);
    				SharedPreferences delCfg = context.getSharedPreferences(delPrefsName,0);
    		        SharedPreferences.Editor delCfgEditor = delCfg.edit();
    		        delCfgEditor.clear();
    		        delCfgEditor.commit();
    		        File file1 = new File("/data/data/pkg.orange.online/shared_prefs/"+delPrefsName+".xml");
    		        file1.delete();
    		        File file2 = new File("/data/data/pkg.orange.online/shared_prefs/"+delPrefsName+".bak");
    		        file2.delete();
                }
            	
            	
            	//do ustawien globalnych id naszego widzetu, zeby potem odczytac ustawienia przy kolejnym dodawaniu
            	globalCfg = getSharedPreferences(PREFS_NAME,0);
            	SharedPreferences.Editor globalEditor = globalCfg.edit();
            	globalEditor.putInt("lastId", appWidgetId);
            	globalEditor.commit();
            	
                // Informujemy widżet o pomyślnej konfiguracji
                Intent resultValue = new Intent();
                resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
                //bez parametru resultValue dostaniemy NullPointerException
                setResult(RESULT_OK, resultValue);
                
                //context.startService(new Intent(context, OrangeUpdateService.class));
                Intent intent = new Intent(context, OrangeUpdateService.class);
				intent.putExtra("appWidgetId", appWidgetId);
				context.startService(intent);
                //Log.d("OrangeConfigureWidget", "Opuszczam...");
                
                finish();
            }
        });
        
	}
	
	
			
}
